#include "user_datasave_tasks.h"

#include "user_tasks_init.h"
#include "ui_DateTimeSetPage.h"

#include "DataSave.h"

void data_save_task(void* pvParameters)
{
	
	while(1)
	{
		
		//printf("\r\n data_save ok?? \r\n");
		
		uint8_t Datastr=0;
		if(xQueueReceive(DataSave_MessageQueue, &Datastr, 1) == pdTRUE)
		{
			/****************
			Setting change
			date change
			Step change
			****************/
			
			uint8_t dat[3];
			dat[0] = HWInterface.IMU.wrist_is_enabled;
			dat[1] = ui_APPSy_EN;
			SettingSave(dat,0x10,2);
			
			RTC_DateTypeDef nowdate;
			HAL_RTC_GetDate(&hrtc,&nowdate,RTC_FORMAT_BIN);
			
			SettingGet(dat,0x20,3);
			if(dat[0] != nowdate.Date)
			{
				if(!HWInterface.IMU.ConnectionError)
					HWInterface.IMU.SetSteps(0);

				dat[0] = nowdate.Date;
				dat[2] = 0;
				dat[1] = 0;
				SettingSave(dat,0x20,3);
			}
			else
			{
				uint16_t temp = HWInterface.IMU.GetSteps();
				dat[0] = nowdate.Date;
				dat[2] = temp & 0xff;
				dat[1] = temp>>8 & 0xff;
				SettingSave(dat,0x20,3);
			}
		}
		
		vTaskDelay(100);
	}
}



